<?php 
class Upload_model extends CI_Model {
	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');
	}

	function upload_text($popis, $mid)
	{

		$uid = $this->session->userdata('id');

		$data = array(
			'uid' 	  => $uid,
			'mid' 	  => $mid,
			'content' => $popis
		);


		$query = $this->db->where('uid', $uid)->where('mid', $mid)->get('solutions');
		$ci = $query->result();

		if (empty($ci[0]->content))
		{
			$this->db->insert('solutions', $data);
		} else {
			$updt = array('content' => $popis);
			$this->db->where('uid', $uid)->where('mid', $mid);
			$this->db->update('solutions', $updt);
		}
	}

	function upload_img($sid, $img)
	{
		$data = array(
			'sid' => $sid,
			'link' => $img
			);

		$this->db->insert('img', $data);
	}

	function delete_img($sid, $img)
	{
		$this->db->where('sid', $sid)->where('link', $img);
			$this->db->delete('img');
	}

	function get_mid($mission)
	{
		$query = $this->db->where('name', $mission)->get('missions');

		foreach ($query->result() as $row)
		{
		    return $row->id;
		}
	}

	function get_sid($mid)
	{
		$uid = $this->session->userdata('id');
		$query = $this->db->where('mid', $mid)->where('uid', $uid)->get('solutions');

		foreach ($query->result() as $row)
		{
		    return $row->id;
		}
	}

	function new_mission($name, $content, $start, $end)
	{

		$data = array(
				'name' => $name,
				'content' => $content,
				'start' => $start,
				'end' => $end
			);

		$this->db->insert('missions', $data);	
	}

	function upload_link($sid, $link)
	{

		$data = array(
			'sid' => $sid,
			'link' => $link
			);

		$this->db->insert('videos', $data);
	}

	function delete_link($sid, $id)
	{
		$this->db->where('sid', $sid)->where('id', $id)->delete('videos');
	}

	function upload_attachment($sid, $cesta)
	{
		$data = array(
			'sid' => $sid,
			'link' => $cesta
			);

		$this->db->insert('attachments', $data);
	}

	function delete_attachment($sid, $cesta)
	{
		$this->db->where('sid', $sid)->where('link', $cesta);
			$this->db->delete('attachments');
	}

}